//
//  DrawCashViewController.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/2.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "DrawCashViewController.h"
#import "CQCountDownButton.h"
#import "JYAddressPicker.h"

#import "WJBaseModel.h"

@interface DrawCashViewController () {
    UIView *_nameView;
    UIView *_phoneView;
    UIView *_codeView;
    BOOL _isVerified; // 是否验证码过
}

@property (nonatomic, strong) UITextField *bankCardTextField;
@property (nonatomic, strong) UITextField *bankNameTextField;
@property (nonatomic, strong) UITextField *bankAreaTextField;
@property (nonatomic, strong) UITextField *bankCardAddressTextField;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *moneyTextField;
@property (nonatomic, strong) UITextField *phoneNumTextField;
@property (nonatomic, strong) UITextField *codeNumTextField;

@property (nonatomic, strong) CQCountDownButton *countDownButton;

/**
 coutDown 点击按钮回调
 */
@property (nonatomic, copy) void(^buttonClick)(void);

/**
 开始计时
 */
@property (nonatomic, copy) void(^countDownStart)(void);

/**
 countDownUnderway
 */
@property (nonatomic, copy) void(^countDownUnderway)(NSInteger);

/**
 countDownCompletion
 */
@property (nonatomic, copy) void(^countDownCompletion)(void);
@end

@implementation DrawCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"我的钱包";
    self.gk_navLineHidden = false;
    self.view.backgroundColor = SKColorWithHex(0xF1F1F1);
    [self initBlocks];
    [self initContentViews];
}


- (void)initContentViews {
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(56.0/667);
    }];
    
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view1);
        make.top.mas_equalTo(view1.mas_bottom).mas_offset(1);
    }];
    
    UIView *view3 = [[UIView alloc] init];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view1);
        make.top.mas_equalTo(view2.mas_bottom).mas_offset(1);
    }];
    
    UIView *view4 = [[UIView alloc] init];
    view4.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view4];
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view1);
        make.top.mas_equalTo(view3.mas_bottom).mas_offset(1);
    }];
    
    UIView *view5 = [[UIView alloc] init];
    view5.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view5];
    [view5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view1);
        make.top.mas_equalTo(view4.mas_bottom).mas_offset(1);
    }];
    
    UIView *view6 = [[UIView alloc] init];
    view6.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view6];
    [view6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(view5);
    }];
    
    UIView *view7 = [[UIView alloc] init];
    view7.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view7];
    [view7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view1);
        make.top.mas_equalTo(view5.mas_bottom).mas_offset(1);
    }];
    _nameView = view5;
    _phoneView = view6;
    _codeView = view7;
    view6.hidden = true;
    view7.hidden = true;
    
    UIView *view8 = [[UIView alloc] init];
    view8.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view8];
    [view8 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view1);
        make.top.mas_equalTo(view5.mas_bottom).mas_offset(1);
    }];
    
    UIButton *drawCashButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [drawCashButton setTitle:@"立即提现" forState:UIControlStateNormal];
    [drawCashButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    drawCashButton.backgroundColor = SKColorWithHex(0xE33030);
    drawCashButton.titleLabel.font = SKFONT_BOLD_SIZE(15);
    [drawCashButton addTarget:self action:@selector(drawCashButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:drawCashButton];
    [drawCashButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(49.0 / 667);
        make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-SKBottomSafe_Height);
    }];
    
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.textColor = SKColorWithHex(0x444444);
    label1.text = @"银行卡号";
    label1.font = SKFONT_Medium(14);
    [view1 addSubview:label1];
    [view1 addSubview:self.bankCardTextField];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(60.0/375);
        make.right.mas_equalTo(self.bankCardTextField.mas_left).mas_offset(-16);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.bankCardTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.textColor = SKColorWithHex(0x444444);
    label2.text = @"开户行";
    label2.font = SKFONT_Medium(14);
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightArrow"]];
    [view2 addSubview:imageView2];
    [view2 addSubview:label2];
    [view2 addSubview:self.bankNameTextField];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.bankNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bankCardTextField);
        make.right.mas_equalTo(imageView2.mas_left).mas_offset(-16);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.centerY.mas_offset(0);
    }];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.textColor = SKColorWithHex(0x444444);
    label3.text = @"开户地区";
    label3.font = SKFONT_Medium(14);
//    UIImageView *imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightArrow"]];
//    [view3 addSubview:imageView3];
    [view3 addSubview:label3];
    [view3 addSubview:self.bankAreaTextField];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.bankAreaTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bankNameTextField);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
//    [imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(imageView2);
//        make.centerY.mas_offset(0);
//    }];
    
    UILabel *label4 = [[UILabel alloc] init];
    label4.textColor = SKColorWithHex(0x444444);
    label4.text = @"银行网点";
    label4.font = SKFONT_Medium(14);
    [view4 addSubview:label4];
    [view4 addSubview:self.bankCardAddressTextField];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.bankCardAddressTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bankCardTextField);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    
    UILabel *label5 = [[UILabel alloc] init];
    label5.textColor = SKColorWithHex(0x444444);
    label5.text = @"姓名";
    label5.font = SKFONT_Medium(14);
    [view5 addSubview:label5];
    [view5 addSubview:self.nameTextField];
    [label5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bankCardTextField);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    
    UILabel *label8 = [[UILabel alloc] init];
    label8.textColor = SKColorWithHex(0x444444);
    label8.text = @"提现金额";
    label8.font = SKFONT_Medium(14);
    [view8 addSubview:label8];
    [view8 addSubview:self.moneyTextField];
    [label8 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.moneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bankCardTextField);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    
    UILabel *label6 = [[UILabel alloc] init];
    label6.textColor = SKColorWithHex(0x444444);
    label6.text = @"手机号";
    label6.font = SKFONT_Medium(14);
    [view6 addSubview:label6];
    [view6 addSubview:self.phoneNumTextField];
    [label6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.phoneNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bankCardTextField);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    
    UILabel *label7 = [[UILabel alloc] init];
    label7.textColor = SKColorWithHex(0x444444);
    label7.text = @"验证码";
    label7.font = SKFONT_Medium(14);
    [view7 addSubview:label7];
    [view7 addSubview:self.codeNumTextField];
    [view7 addSubview:self.countDownButton];
    [label7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.codeNumTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bankCardTextField);
        make.right.mas_equalTo(self.countDownButton.mas_left).mas_offset(-16);
        make.top.mas_offset(8);
        make.centerY.mas_offset(0);
    }];
    [self.countDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.centerY.mas_offset(0);
        make.top.mas_offset(8);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(90.0/375);
    }];
    
//    UIButton *chooseBankNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [chooseBankNameBtn addTarget:self action:@selector(chooseBankNameClick:) forControlEvents:UIControlEventTouchUpInside];
//    [view2 addSubview:chooseBankNameBtn];
//    [chooseBankNameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(view2);
//    }];
    
//    UIButton *chooseBankAreaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [chooseBankAreaBtn addTarget:self action:@selector(chooseBankAreaClick:) forControlEvents:UIControlEventTouchUpInside];
//    [view3 addSubview:chooseBankAreaBtn];
//    [chooseBankAreaBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(view3);
//    }];
    
    imageView2.hidden = true;
}


#pragma mark -- mark 懒加载

- (UITextField *) bankCardTextField {
    if (!_bankCardTextField) {
        _bankCardTextField = [[UITextField alloc] init];
        _bankCardTextField.placeholder = @"请输入银行卡号（仅限储蓄卡）";
        _bankCardTextField.textColor = SKColorWithHex(0x444444);
        _bankCardTextField.font = SKFONT_Medium(12);
        _bankCardTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _bankCardTextField;
}
- (UITextField *)bankNameTextField {
    if (!_bankNameTextField) {
        _bankNameTextField = [[UITextField alloc] init];
        _bankNameTextField.placeholder = @"请输入开户行";
        _bankNameTextField.textColor = SKColorWithHex(0x444444);
        _bankNameTextField.font = SKFONT_Medium(12);
        _bankNameTextField.keyboardType = UIKeyboardTypeDefault;
//        _bankNameTextField.enabled = false;
    }
    return _bankNameTextField;
}
- (UITextField *)bankAreaTextField {
    if (!_bankAreaTextField) {
        _bankAreaTextField = [[UITextField alloc] init];
        _bankAreaTextField.placeholder = @"请选择开户行地区";
        _bankAreaTextField.textColor = SKColorWithHex(0x444444);
        _bankAreaTextField.font = SKFONT_Medium(12);
        _bankAreaTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _bankAreaTextField;
}
- (UITextField *) bankCardAddressTextField {
    if (!_bankCardAddressTextField) {
        _bankCardAddressTextField = [[UITextField alloc] init];
        _bankCardAddressTextField.placeholder = @"请输入银行网点";
        _bankCardAddressTextField.textColor = SKColorWithHex(0x444444);
        _bankCardAddressTextField.font = SKFONT_Medium(12);
        _bankCardAddressTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _bankCardAddressTextField;
}
- (UITextField *) nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] init];
        _nameTextField.placeholder = @"请输入您的姓名";
        _nameTextField.textColor = SKColorWithHex(0x444444);
        _nameTextField.font = SKFONT_Medium(12);
        _nameTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _nameTextField;
}
- (UITextField *)moneyTextField {
    if (!_moneyTextField) {
        _moneyTextField = [[UITextField alloc] init];
        _moneyTextField.placeholder = @"请输入提现金额";
        _moneyTextField.textColor = SKColorWithHex(0x444444);
        _moneyTextField.font = SKFONT_Medium(12);
        _moneyTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _moneyTextField;
}
- (UITextField *) phoneNumTextField {
    if (!_phoneNumTextField) {
        _phoneNumTextField = [[UITextField alloc] init];
        _phoneNumTextField.placeholder = @"请输入您的手机号";
        _phoneNumTextField.textColor = SKColorWithHex(0x444444);
        _phoneNumTextField.font = SKFONT_Medium(12);
        _phoneNumTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _phoneNumTextField;
}
- (UITextField *) codeNumTextField {
    if (!_codeNumTextField) {
        _codeNumTextField = [[UITextField alloc] init];
        _codeNumTextField.placeholder = @"请输入验证码";
        _codeNumTextField.textColor = SKColorWithHex(0x444444);
        _codeNumTextField.font = SKFONT_Medium(12);
        _codeNumTextField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _codeNumTextField;
}
- (CQCountDownButton *)countDownButton {
    if (!_countDownButton) {
        _countDownButton = [[CQCountDownButton alloc] initWithDuration:10 buttonClicked:self.buttonClick countDownStart:self.countDownStart countDownUnderway:self.countDownUnderway countDownCompletion:self.countDownCompletion];
        [_countDownButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_countDownButton setTitleColor:SKColorWithHex(0x4444444) forState:UIControlStateNormal];
        [_countDownButton setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateDisabled];
        _countDownButton.titleLabel.font = SKFONT_Semibold(15);
    }
    return _countDownButton;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:true];
}
#pragma mark -- mark Blocks

- (void)initBlocks {
    SKWeakSelf;
    self.buttonClick = ^{
        // 点击计时按钮
        if (!(weakSelf.phoneNumTextField.text.length > 10)) {
            return ; }
        [SVProgressHUD showWithStatus:@"正在获取验证码..."];
        [YQNetworking postWithUrl:@"Login/sendsms" refreshRequest:YES cache:NO params:@{@"mobile":weakSelf.phoneNumTextField.text,@"sign":@"yupinyouxuan*****yupinyouxuan"} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        } successBlock:^(id response) {
            NSDictionary *dic = (NSDictionary*)response;
            if ([dic[@"status"] isEqualToString:@"ok"])
            {
                [SVProgressHUD showSuccessWithStatus:@"验证码已发送"];
                [weakSelf.countDownButton startCountDown];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:@"获取失败，请重试"];
                weakSelf.countDownButton.enabled = YES;
            }
        } failBlock:^(NSError *error) {
            
        }];
    };
    self.countDownStart = ^{
        // 开始计时
        //------- 倒计时开始 -------//
        
    };
    self.countDownUnderway = ^(NSInteger index) {
        // TODO:
        //------- 倒计时进行中 -------//
        [weakSelf.countDownButton setTitle:[NSString stringWithFormat:@"重新获取(%ld)", index] forState:UIControlStateNormal];
    };
    self.countDownCompletion = ^{
        //------- 倒计时结束 -------//
        [weakSelf.countDownButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        NSLog(@"倒计时结束");
    };
}
#pragma mark -- mark EVENT

- (void)chooseBankNameClick:(UIButton *)sender {
    
}
- (void)chooseBankAreaClick:(UIButton *)sender {
    SKWeakSelf;
    JYAddressPicker *addressPicker = [JYAddressPicker jy_showAt:self];
    addressPicker.selectedItemBlock = ^(NSArray *addressArr) {
        NSString *province = [addressArr objectAtIndex:0][@"text"];
        NSString *city = [addressArr objectAtIndex:1][@"text"];
        NSString *county = [addressArr objectAtIndex:2][@"text"];
        SKLog(@"%@",[NSString stringWithFormat:@"%@%@%@",province,city,county]);
        weakSelf.bankAreaTextField.text = [NSString stringWithFormat:@"%@%@%@",province,city,county];
    };
}
- (void)drawCashButtonClick:(UIButton *)sender {
    BOOL cardNumber = self.bankCardTextField.text.length > 0;
    BOOL cardName = self.bankNameTextField.text.length > 0;
    BOOL cardArea = self.bankAreaTextField.text.length > 0;
    BOOL cardAddress = self.bankCardAddressTextField.text.length > 0;
    BOOL realName = self.nameTextField.text.length > 0;
    BOOL phoneNum = self.phoneNumTextField.text.length > 0;
    BOOL codeNum = self.codeNumTextField.text.length > 0;
    BOOL money = self.moneyTextField.text.length > 0;
    
    if (!(cardNumber && cardName && cardArea && cardAddress && realName && money)) {
        return; }
    // TODO: 提现
    double total_money = [UserInfoSingleton shareInstance].userInfo.keyong_yue.doubleValue;
    double draw_money = [self.moneyTextField.text doubleValue];
    if (draw_money < 1) {
        [self showMessage:@"提现金额不能低于1元"];
        return; }
    if (!(total_money > 1)) {
        [self showMessage:@"余额不足！"];
        return; }
    if (draw_money > total_money) {
        [self showMessage:@"余额不足！"];
        return; }
    [self drawCashWithRealName:self.nameTextField.text BankCard:self.bankCardTextField.text BankOf:self.bankNameTextField.text BankCity:self.bankAreaTextField.text BankAddress:self.bankCardAddressTextField.text Money:[NSString stringWithFormat:@"%.2f", draw_money]];
    /*
    if (!_isVerified) {
        if (!(cardNumber && cardName && cardArea && cardAddress && realName)) {
            return; }
        _isVerified = true;
        _nameView.hidden = true;
        _phoneView.hidden = false;
        _codeView.hidden = false;
        self.bankCardTextField.enabled = false;
        self.bankNameTextField.enabled = false;
        self.bankAreaTextField.enabled = false;
        self.bankCardAddressTextField.enabled = false;
    }else {
        if (!(phoneNum && codeNum)) {
            return; }
        // TODO: 提现
        double money = [UserInfoSingleton shareInstance].userInfo.keyong_yue.doubleValue;
        if (!(money > 1)) {
            [self showMessage:@"余额不足！"];
            return; }
        [self drawCashWithBankName:self.bankNameTextField.text BankCard:self.bankCardTextField.text Money:[NSString stringWithFormat:@"%.2f", money]];
    }
     */
}




#pragma mark -- mark 网络请求

/**
 提现

 @param realName 真实姓名
 @param cardNum 银行卡号
 @param money 金额
 */
- (void)drawCashWithRealName:(NSString *)realName BankCard:(NSString *)cardNum BankOf:(NSString *)bankOf BankCity:(NSString *)bankCity BankAddress:(NSString *)bankAddress Money:(NSString *)money {
    NSDictionary *param = @{@"uid":self.uid,
                            @"loginKey":self.loginKey,
                            @"bank_name":realName,
                            @"bank_card":cardNum,
                            @"money":money,
                            @"bank_of":bankOf,
                            @"bank_city":bankCity,
                            @"bank_addr":bankAddress
    };
    SKWeakSelf;
    [YQNetworking getWithUrl:@"pay/tixian" params:param reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        WJBaseModel *model = [WJBaseModel modelWithDictionary:response];
        [weakSelf showMessage:model.msg];
        if ([model isSuccess]) {
            // 提现成功
            if (weakSelf.drawCashResultBlock) {
                weakSelf.drawCashResultBlock();
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:true];
            });
        }
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}
@end
